package com.whmnx.main.ui.model;

import android.app.Application;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.CommonRecyclerAdapter;
import com.whmnx.base.bean.MsgBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.main.BR;
import com.whmnx.main.R;
import com.whmnx.main.databinding.FragmentMsgBinding;
import com.whmnx.main.ui.activity.MessageActivity;
import com.whmnx.main.ui.activity.MsgDetailActivity;
import com.whmnx.main.ui.adapter.MsgAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.utils.SPUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/9/25 0025 18:13
 * version: 1.0
 */
public class MsgModel extends ToolbarViewMainModel{

    MessageActivity mActivity;
    FragmentMsgBinding mBinding ;
    private MsgAdapter msgAdapter;

    public MsgModel(@NonNull Application application) {
        super(application);

    }


    //给RecyclerView添加ObservableList
    public ObservableList<MsgItemViewModel> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<MsgItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_msg);


    public void initToolbar(MessageActivity messageActivity, FragmentMsgBinding binding) {
        this.mActivity = messageActivity;
        this.mBinding = binding;
        setTitleText("消息");

        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        //配置布局，默认为vertical（垂直布局），下边这句将布局改为水平布局
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mBinding.rlList.setLayoutManager(layoutManager);
        mBinding.rlList.setItemAnimator(new DefaultItemAnimator());
        msgAdapter = new MsgAdapter(mActivity);
        mBinding.rlList.setAdapter(msgAdapter);
        initView();

        msgAdapter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
                Bundle bundle = new Bundle();
                 bundle.putString("id",msgAdapter.getItem(i).getMessage_Id());
                 startActivity(MsgDetailActivity.class,bundle);
            }
        });
    }

    private void initView() {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", SPUtils.getInstance().getString(Constants.USERID));
        params.put("pageIndex",1);
        params.put("pageSize",20);
        HttpClient.post(mActivity, Constants.MESSAGELIST, params,new CallBack<List<MsgBean>>() {
            @Override
            public void onSuccess(List<MsgBean> msgBeans) {
                msgAdapter.replaceAll(msgBeans, true);
             //   observableList.clear();
                for (MsgBean msgBean : msgBeans) {
                  //  observableList.add(new MsgItemViewModel(MsgModel.this,msgBean));
                }
            }
        });
    }
}
